package com.bigstar.lolkor;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class ReflectImageView extends ImageView{

	public ReflectImageView(Context context){
		super(context);
	}
	
	public ReflectImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	@Override
	public void setImageDrawable(Drawable drawable) {

		final Matrix m = new Matrix();
		final Paint paint = new Paint();
		final PorterDuffXfermode mode = new PorterDuffXfermode(Mode.DST_IN);
		final Canvas c = new Canvas();		
		
		//recycle old bitmap.
		Drawable old = this.getDrawable();
		if(old != null && old instanceof BitmapDrawable){
			((BitmapDrawable)old).getBitmap().recycle();
		}
		
		if(drawable instanceof BitmapDrawable){
			Bitmap bm = ((BitmapDrawable)drawable).getBitmap();
			int w = bm.getWidth();
			int h = bm.getHeight();
			Bitmap temp = Bitmap.createBitmap(w, h * 2, bm.getConfig());
			c.setBitmap(temp);
			m.reset();
			c.drawBitmap(bm, m, null);
			m.postScale(1, -1);
			m.postTranslate(0, h * 2);
			c.drawBitmap(bm, m, null);
			
			LinearGradient gradient = new LinearGradient(0, h, 0, h + h / 2,
					0x70ffffff, 0x00ffffff, TileMode.CLAMP);
			paint.setShader(gradient);
			paint.setXfermode(mode);
			c.drawRect(0, h, w, h * 2, paint);			
			BitmapDrawable bd = new BitmapDrawable(getContext().getResources(), temp);
			super.setImageDrawable(bd);
		}else
			super.setImageDrawable(drawable);
	}
	
	@Override
	public void setImageResource(int resId) {
		
		Drawable d = getContext().getResources().getDrawable(resId);
		if(d instanceof BitmapDrawable)
			setImageDrawable(d);
		else
			super.setImageResource(resId);
	}

}//